*12345678901234567890123456789012345678901234567890123456789012345678901234567890
capture log close
clear all
set more off

*	************************************************************************
* 	File-Name: 	TrumpAnalysis.do
*	Log-file:	na
*	Date:  		07/15/2018
*	Author: 	Michaël Aklin 
*	Data Used:  Data_A.dta
*	Output		tables, figures
*	Purpose:   	.do file to replicate the main results of:
*				Aklin, Michaël. 2018. "How Robust Is the Renewable Energy 
*				Industry to Political Shocks? Evidence from the 2016 U.S.
*				Elections" Business & Politics. 
*
*				Change the data folder to run this file.
*	************************************************************************


*	************************************************************************
*	A. Data
*	************************************************************************

*	UPDATE WORKING DIRECTORY
cd "" 
clear all

use "Data.dta", clear


*	************************************************************************
*	B. Analysis
*	************************************************************************

*	************************************************************************
*	B.0. Summary stats 
*	************************************************************************

preserve
quiet reg car elecd if index == 0 & pool == 1 & eventwindow==1
drop if e(sample)==0
estpost sum return abnormalreturn car type_wind type_solar type_biofuel ///
	type_hydro type_cleanother, d
esttab using "./Tables/SummaryStats.tex", ///
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
eststo clear
restore

list firmname headquarters if index == 0 & pool == 1 & date == mdy(11,8,2016)
tab ticker if index == 0 & pool == 1 & date == mdy(11,8,2016)


*	************************************************************************
*	B.1. Table 3
*	************************************************************************

eststo: reg dreturn lreturn delecd lelecd if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg dreturn lreturn delecd lelecd i.ccode if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg dreturn lreturn delecd lelecd i.hqn i.typecategory  if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)

eststo: reg dabnormalreturn labnormalreturn delecd lelecd if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg dabnormalreturn labnormalreturn delecd lelecd i.ccode if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg dabnormalreturn labnormalreturn delecd lelecd i.hqn i.typecategory if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)

esttab using "Table_3.tex"	///
	,  booktabs label replace ///
 	nodepvars se(2) b(2) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	compress nogaps r2(2)  ///
	scalars("N_clust \# Clusters") sfmt(%9.0f)	///
	mtitles("OLS" "FE" "FE" "OLS" "FE" "FE")	///
	mgroups("Observed Returns" "Abnormal Returns", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))	///
	noconstant nonotes	///
	order(delecd lelecd)	///
	indicate("Firm FE = *ccode*" "Headquarter FE = *hqn*"  "Sector FE = *typecategory*", label($\checkmark$ ))	
eststo clear

*	************************************************************************
*	B.2. Table 4
*	************************************************************************

eststo: reg return elecd if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg return elecd i.ccode if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg return elecd i.hqn i.typecategory if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)

eststo: reg abnormalreturn elecd if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg abnormalreturn elecd i.ccode if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg abnormalreturn elecd i.hqn i.typecategory if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)

eststo: reg car elecd if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg car elecd i.ccode if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)
eststo: reg car elecd i.hqn i.typecategory if index == 0 & pool == 1 & eventwindow==1, cluster(ccode)

esttab using "Table_4.tex"	///
	,  booktabs label replace ///
 	nodepvars se(2) b(2) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	compress nogaps r2(2)  ///
	scalars("N_clust \# Clusters") sfmt(%9.0f)	///
	mtitles("OLS" "FE" "FE" "OLS" "FE" "FE" "OLS" "FE" "FE")	///
	mgroups("Observed Returns" "Abnormal Returns" "Cumulative Abnormal Returns", pattern(1 0 0 1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))	///
	noconstant nonotes	///
	order(elecd)	///
	indicate("Firm FE = *ccode*" "Headquarter FE = *hqn*"  "Sector FE = *typecategory*", label($\checkmark$ ))	
eststo clear


*	************************************************************************
*	B.3. Table 5
*	************************************************************************

eststo: reg dreturn lreturn delecd lelecd i.ccode if index == 0 & pool == 1 & us == 1 & eventwindow==1, cluster(ccode)
eststo: reg dabnormalreturn labnormalreturn delecd lelecd i.ccode if index == 0 & pool == 1 & us == 1 & eventwindow==1, cluster(ccode)
eststo: reg dcar lcar delecd lelecd i.ccode if index == 0 & pool == 1 & us == 1 & eventwindow==1, cluster(ccode)

eststo: reg dreturn lreturn delecd lelecd i.ccode if index == 0 & pool == 1 & us == 0 & eventwindow==1, cluster(ccode)
eststo: reg dabnormalreturn labnormalreturn delecd lelecd i.ccode if index == 0 & pool == 1 & us == 0 & eventwindow==1, cluster(ccode)
eststo: reg dcar lcar delecd lelecd i.ccode if index == 0 & pool == 1 & us == 0 & eventwindow==1, cluster(ccode)

esttab using "Table_5.tex"	///
	,  booktabs label replace ///
 	nodepvars se(2) b(2) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	compress nogaps r2(2)  ///
	scalars("N_clust \# Clusters") sfmt(%9.0f)	///
	mtitles("OR" "AR" "CAR" "OR" "AR" "CAR")	///
	mgroups("US" "Rest of the World", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))	///
	noconstant nonotes	///
	order(delecd lelecd)	///
	indicate("Firm FE = *ccode*" , label($\checkmark$ ))	
eststo clear


*	************************************************************************
*	C. Graphs
*	************************************************************************


*	************************************************************************
*	C.1. Figure 2
*
*	Note that the original figure was created in R.
*	************************************************************************

preserve
* Create the mean for the sample being reported in the graph
bysort date (ccode): egen mean_ar = mean(abnormalreturn) if index == 0 & pool == 1 & eventwindow==1 & ticker != "OPTT"

xtline abnormalreturn if index == 0 & pool == 1 & eventwindow==1 & ticker != "OPTT", 	///
	overlay addplot(tsline mean_ar if eventwindow==1, lwidth(thick) lcolor(black)) /// Add the mean
	xline(20766, lcolor(black) lpattern(solid))	///
	legend(off)
graph export "Figure_2_Panel_A.pdf", replace as(pdf)


xtline abnormalreturn if index == 0 & pool == 1 & eventwindow==1 & ticker != "OPTT" & (date > mdy(11,1,2016) & date < mdy(11,15,2016)), 	///
	overlay addplot(tsline mean_ar if eventwindow==1  & (date > mdy(11,1,2016) & date < mdy(11,15,2016)), lwidth(thick) lcolor(black)) ///
	xline(20766, lcolor(black) lpattern(solid))	///
	legend(off)
graph export "Figure_2_Panel_B.pdf", replace as(pdf)

restore

*	************************************************************************
*	C.2. Figure 3
*	************************************************************************

preserve
drop if index == 1 | pool == 0
drop if eventwindow==0
keep if us == 1
collapse (mean) car (median) car_median = car, by(date)
label var car "Mean CAR"
label var car_median "Median CAR"
list car date if date >  mdy(11,18,2016) & date < mdy(12,1,2016)
twoway (tsline car)	///
	(tsline car_median)	///
	,	///
	tline(08nov2016, lpattern(solid))	///
	yline(0, lpattern(solid))	///
	xtitle("Days")	///
	ytitle("CAR")	///
	tlabel(, format(%tdmd) )	///
	legend(position(6)  col(2))	
graph export "Figure_3_Panel_A.pdf", replace as(pdf)
restore

preserve
drop if index == 1 | pool == 0
drop if eventwindow==0
keep if us == 0
collapse (mean) car (median) car_median = car, by(date)
label var car "Mean CAR"
label var car_median "Median CAR"
list car date if date >  mdy(11,18,2016) & date < mdy(12,1,2016)
twoway (tsline car)	///
	(tsline car_median)	///
	,	///
	tline(08nov2016, lpattern(solid))	///
	yline(0, lpattern(solid))	///
	xtitle("Days")	///
	ytitle("CAR")	///
	tlabel(, format(%tdmd) )	///
	legend(position(6)  col(2))	
graph export "Figure_3_Panel_B.pdf", replace as(pdf)
restore


twoway (kdensity car if date >= mdy(11,9,2016) & us == 1 & eventwindow == 1)	///
(kdensity car if date >= mdy(11,9,2016) & us == 0 & eventwindow == 1, lpattern(dash))	///
	, 	///
	legend(position(6) label(1 "US") label(2 "Non-US") col(2))	///
	xtitle("CAR (post-election)")	///
	ytitle("Kernel Density")
graph export "Figure_3_Panel_C.pdf", replace as(pdf)


